//
//  FGTabBarController.swift
//  FGApp
//
//  Created by ryan on 2019/4/26.
//  Copyright © 2019 ryan. All rights reserved.
//

import UIKit


class FGTabBarController: UITabBarController, FGTabBarDelegate{

    // from plist
    var tabBarConfigArr : [Dictionary <String, String>]!
    
    var fgTabBarView : FGTabBarView!
    
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nil, bundle: nil)
        self.tabBarConfigArr = self.getConfigFromPlist()
        // 创建视图控制器
        self.createControllers()
        // 创建自定义TabBarView
        self.createMainTabBarView()
    }
    
    required init?(coder aDecoder: NSCoder){
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
//        addObserves()
    }
    
    public func setTabBarHidden(isHidden: Bool) {
        if isHidden {
            fgTabBarView.isHidden = isHidden
        }else {
            fgTabBarView.alpha = 0
            fgTabBarView.isHidden = isHidden
            UIView.animate(withDuration: 0.15) {
                self.fgTabBarView.alpha = 1
            }
        }
    }
    
    //MARK: - Private Methods
    private func createControllers() {
        var controllerNameArray = [String]()
        var controllerTitle = [String]()
        
//        for i in 0...self.tabBarConfigArr.count-1 {
//        }
        
        for i in 0...self.tabBarConfigArr.count-1 {
           
            let dictionary = tabBarConfigArr[i]
                controllerNameArray.append(dictionary["className"]!)
                controllerTitle.append(dictionary["titleName"]!)
            
            guard controllerNameArray.count > 0 else{
                print("error:控制器数组为空")
                return
            }
            
            var nvcArray = [FGNavigatioController]()
            
            let clsName = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
            
            for i in 0...controllerNameArray.count - 1 {
                let anyClass: AnyClass? = NSClassFromString(clsName + "." + controllerNameArray[i])
                // 转cController
                if let vcClassType = anyClass as? FGTabBarBaseController.Type {
                    let viewController = vcClassType.init()
                    viewController.title = controllerTitle[i]
                    let nav = FGNavigatioController(rootViewController: viewController)
                    nvcArray.append(nav)
                }
            }
            self.viewControllers = nvcArray
        }
    }
    
    private func createMainTabBarView() {
//        let tabBarRect = self.tabBar.frame
        self.tabBar.isHidden = true
        // 初始化
        fgTabBarView = FGTabBarView (frame: KTabBarBottomFrame, tabBarConfigArr: tabBarConfigArr!)
        
        fgTabBarView.delegate = self
        view.addSubview(fgTabBarView)
    }
    
    
    private func getConfigFromPlist () -> ([Dictionary <String, String>]?) {
        let filePath : String? = Bundle.main.path(forResource: "FGBaseConfig", ofType: "plist")
        let plistData = NSDictionary(contentsOfFile: filePath ?? "")
        
        var config : [Dictionary<String, String>]? = []
        config = plistData?.object(forKey: "TabBarArray") as? [Dictionary <String, String>]
        
        return config
    }
    
    //MARK: - MainTabBarDelegate
    func didSelectItem(itemIndex: Int) {
        self.selectedIndex = itemIndex
    }
    
    func addObserves() {
        
    }

    
    func didSelectMid() {
        
    }
}
